{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cuda is  True\n",
      "Cuda num is  0\n"
     ]
    },
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'cv2'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-361ac565ec7a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mMinPy\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtask\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtorch\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mt\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mMinPy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtoolbox\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mplot\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mplot_tra\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mautograd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_detect_anomaly\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\jamily\\Documents\\GitHub\\rmnn\\MinPy\\task.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mtoolbox\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprojection\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0msvd_pro\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mmask_pro\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mtoolbox\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mdataloader\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mplot\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mpprint\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     18\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mreg\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdemo\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mloss\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     19\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\jamily\\Documents\\GitHub\\rmnn\\MinPy\\toolbox\\dataloader.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     12\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdata\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mTensorDataset\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdata\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mDataLoader\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     15\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'cv2'"
     ]
    }
   ],
   "source": [
    "from MinPy import task\n",
    "import torch as t\n",
    "from MinPy.toolbox.plot import plot_tra\n",
    "import matplotlib.pyplot as plt\n",
    "t.autograd.set_detect_anomaly(True)\n",
    "\n",
    "# m,n分别定义读取的图片的长宽，不用管原始是多少，这里会rescale到相应的大小\n",
    "m,n = 256,256\n",
    "# 第一类神经网络，神经网络输出本身就是矩阵：\n",
    "# 1. dmf:深度矩阵分解模型\n",
    "# 2. dip:Deep Image Prior\n",
    "# 3. fc: 全连接神经网络组成的AutoEncoderDecoder\n",
    "# model_name = 'dip' #'dip','fc'\n",
    "# para = [m,m,n,n]\n",
    "\n",
    "# 第二类神经网络，从坐标映射到矩阵值：\n",
    "# 1. fp: F-Principle, ReLU激活的全连接神经网络， R^2 -> R^1\n",
    "# 2. fourier: multiply filter network ，当filter为傅里叶基的情况\n",
    "# 3. garbor: multiply filter network ，当filter为Garbor基的情况\n",
    "model_name = 'fp'#'fourier'#'garbor'\n",
    "model_name = 'siren'\n",
    "para = [2,256,1]\n",
    "\n",
    "# model_name = 'mulbacon'\n",
    "# para = [256,128,4]\n",
    "\n",
    "# model_name = 'msn'\n",
    "# para = [m,n]\n",
    "\n",
    "# 第三类神经网络，混合\n",
    "# model_name = 'multi_net'\n",
    "# para = [m,m,n,n]\n",
    "\n",
    "# 读取图片的路径设置，可自行替换\n",
    "data_path='./train_pics/Cameraman.jpg'\n",
    "# 设置mask的模式，\n",
    "# 为random时为随机丢失，和之后的random_rate相配合。\n",
    "# 为patch时，为丢失固定位置的像素，无需额外设置。\n",
    "# 为fixed时需要设置mask_path确定掩码图像\n",
    "mask_mode='random'#'patch_num' # 'patch','fixed'\n",
    "# 当mask_mode为fixed时启用，读取掩码矩阵\n",
    "mask_path='./masks/mask.png'\n",
    "# 当mask_mode为random时启用，为丢失像素值的比例 [0,1]\n",
    "random_rate=0\n",
    "# 正则项的添加，包含四种正则项\n",
    "# 使用 None 代表无需正则\n",
    "# 使用 'TV' 代表使用默认参数的TV正则\n",
    "# 使用 'AIR' 代表使用默认参数的AIR自适应正则\n",
    "# 使用 'NN' 代表使用一个卷积神经网络正则\n",
    "# 使用 'eta' 代表自定义混合正则，由四项系数控制，分别控制['TV','row of AIR','col of AIR','NN']正则的系数，当填None时代表不使用该正则\n",
    "reg_mode = None # 'TV',''AIR','NN',None,'eta':custom\n",
    "# 使用 'eta' 时启用\n",
    "eta = [1e2,None,None,None]\n",
    "\n",
    "# 训练批次\n",
    "epoch=1001\n",
    "\n",
    "# 定义训练任务，在task.py中定义，对于去噪或是其他逆问题只需要修改这个函数即可\n",
    "task_dmf = task.shuffle_task(m=m,n=n,data_path=data_path,mask_mode=mask_mode,random_rate=random_rate,\n",
    "                     mask_path=mask_path,given_mask=None,para=para,input_mode='masked',\n",
    "                    std_b=1e-3,reg_mode=reg_mode,model_name=model_name,pro_mode='mask',\n",
    "                                 opt_type='Adam',shuffle_mode='I',verbose=True,std_w=1e-3,act='tanh',\n",
    "                             patch_num=10,net_list=['fp','garbor'],n_layers=8,scale_factor=2,model_load_path=None,\n",
    "                             task_type='denoising',noise_dict={'type':'gaussian','sigma':0.1,'SNR':0.1,'lam':0.1},sample_mode='random')\n",
    "# 对定义的训练任务进行训练\n",
    "# 通过print_epoch,imshow_epoch参数调节每隔多少次输出一次\n",
    "# 通过imshow 调节是否输出图像\n",
    "task_dmf.train(epoch=epoch,print_epoch=100,imshow_epoch=100,plot_mode='gray',\n",
    "                   verbose=True,imshow=True,stop_err=None,eta=eta,\n",
    "                   model_save_path='./save_models/test.pth',model_save=False,\n",
    "                   fid_name=None)\n",
    "\n",
    "# 绘制出收敛过程训练误差(Obs_MSE)和测试误差(Unk_MSE)的变化情况\n",
    "plot_tra(pro_list=task_dmf.pro_list,color='r',label='MSE',pointsize=50)\n",
    "plt.xlabel('Obs_MSE')\n",
    "plt.ylabel('Unk_MSE')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.0 64-bit ('Anaconda3')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "vscode": {
   "interpreter": {
    "hash": "3641e4d6ea0870ed0162470857a6b99e659f9af357693a1afe22018e42a2dc9b"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
